<?php
namespace Api\Controller;
//个人资料-相册-视频-日记
class SelfController extends ApiController{
    public function __construct(){
        parent::__construct();
        /** 测试不判断登录 modify by zhang 2016 start  **/
        if (!I('mytest')) {
            !api_login() && response(-94);
            !passed_login() && response(-95);
            !only_login() && response(-96);
        }
        /** 测试不判断登录 modify by zhang 2016 end **/
    }
    //相册列表
    public function thumbIndex() {
        $uid =  I('uid');
        $result = M('self_thumb')->where('uid='.$uid)->select();
        //获取相册地址
        foreach($result as $key => $value) {
            if (empty($value['cover_ids'])){
                continue;
            }
            $cover_ids= explode(',', $value['cover_ids']);
            foreach ($cover_ids as $key1=>$value1) {
                $result[$key]['imgpath'][$key1] = M('picture')->where('id='.$value1)->getField('path');
            }
        }
        if ($result) {
            response(1, $result);
        } else {
            response(-9);
        }
    }
    //添加相册
    public function thumbAdd(){
        $data = array(
            'uid'=>API_UID,
            'content'=>I('post.content'),
            'create_time'=>NOW_TIME,
            'cover_ids' =>I('post.cover_ids')
        );
        $result  = M('self_thumb')->add($data);
        if ($result) {
            response(1, $result);
        } else {
            response(-9);
        }
    }
    //删除相册
    public function thumbDelete(){
        $id = I('post.id');
        $result = M('self_thumb')->where('id='.$id)->delete();
        if ($result) {
            response(1, $result);
        } else {
            response(-9);
        }
    }

    //视频列表
    public function movieIndex() {
        $result = M('self_movie')->where('uid='.API_UID)->select();
        if ($result) {
            foreach ($result as $key=>$val) {
                $result[$key]['moviePath'] = M('movie')->where('id='.$val['movie_id'])->getField('savepath');
            }
            response(1, $result);
        } else {
            response(-9);
        }
    }
    //添加视频
    public function movieAdd(){
        $data = array(
            'uid'=>API_UID,
            'content'=>I('post.content'),
            'create_time'=>NOW_TIME,
            'movie_id' =>I('post.movie_id')
        );
        $result  = M('self_movie')->add($data);
        if ($result) {
        response(1, $result);
        } else {
            response(-9);
        }
    }
    //删除视频
    public function movieDelete(){
        $id = I('post.id');
        $result = M('self_movie')->where('id='.$id)->delete();
        if ($result) {
            response(1, $result);
        } else {
            response(-9);
        }
    }


    //日记列表
    public function diaryIndex() {
        $result = M('self_diary')->where('uid='.API_UID)->select();
        if ($result) {
            response(1, $result);
        } else {
            response(-9);
        }
    }
    //添加日记
    public function diaryAdd(){
        $data = array(
            'uid'=>API_UID,
            'content'=>I('post.content'),
            'create_time'=>NOW_TIME,
        );
        $result  = M('self_diary')->add($data);
        if ($result) {
            response(1, $result);
        } else {
            response(-9);
        }
    }
    //删除日记
    public function diaryDelete(){
        $id = I('post.id');
        $result = M('self_diary')->where('id='.$id)->delete();
        if ($result) {
            response(1, $result);
        } else {
            response(-9);
        }
    }

    //三合一列表
    public function index() {
        $uid = I('uid');
        $result = M('self')->where('uid='.$uid)->order('create_time desc')->select();
        //获取相册地址
        foreach($result as $key => $value) {
            if (!empty($value['cover_ids'])) {
                $cover_ids= explode(',', $value['cover_ids']);
                foreach ($cover_ids as $key1=>$value1) {
                    $picture = M('picture')->where('id='.$value1)->getField('path');
                    $result[$key]['imgpath'][$key1] = $picture?$picture:'';
                }
            } else {
                $result[$key]['imgpath']=array();
            }
            $movie = M('movie')->where('id='.$value['movie_id'])->order('create_time desc')->getField('savepath');
            $result[$key]['moviePath'] = $movie?$movie:'';
        }
        if ($result) {
            response(1, $result);
        } else {
            response(-9);
        }
    }
    //三合一日记
    public function add(){
        $data = array(
            'uid' => API_UID,
            'content' => I('post.content'),
            'create_time'=> NOW_TIME,
            'movie_id' => I('post.movie_id'),
            'cover_ids' => I('post.cover_ids'),
            'type' => I('type')
        );
        $result  = M('self')->add($data);
        if ($result) {
            response(1, $result);
        } else {
            response(-9);
        }
    }
    //三合一日记
    public function delete(){
        $id = I('post.id');
        $result = M('self')->where('id='.$id)->delete();
        if ($result) {
            response(1);
        } else {
            response(-9);
        }
    }
    /**
     * 三合一权限设置
     */
    public function setAuth() {
        $uid = API_UID;
        $type = I('type');
        $info = M('member')->where('uid='.$uid)->getField('public');
        if ($info != $type) {
            M('member')->where('uid='.$uid)->setField('public', $type);
        }
        response(1);
    }
    /**
     * 判断是否有权限查看朋友圈
     */
    public function hasAuth() {
        $uid = API_UID;
        $fid = I('fid');
        $auth = M('member')->where('uid='.$fid)->getField('public');
        $where['fid'] = $fid;
        $where['uid'] = $uid;
        $is_friend = M('user_friend')->where($where)->find();
        $where1['uid'] = $fid;
        $where1['fid'] = $uid;
        $is_friend1 = M('user_friend')->where($where1)->find();
        if ($auth == 1 && $is_friend && $is_friend1) {
            response(1);
        } else {
            response(-9);
        }
    }
}